/*global window: false*/
/*
 * #depends subsonic-login.js
 */
Ext.ns('Ext.ux.mattgoldspink.subsonic.controllers');

Ext.ux.mattgoldspink.subsonic.controllers.Store = function(){
    // setup msg bus
    this.msgbus = new Ext.ux.MsgBus();
    this.msgbus.init(this);
    this.subscribe('subsonic.folder.click', {
        scope: this,
        fn: this.handleFolderClick
    });
    this.subscribe('subsonic.load.store', {
        scope: this,
        fn: this.handleLoadStore
    });
	this.subscribe('subsonic.search.store', {
        scope: this,
        fn: this.handleSearch
    });
	this.playlistJsonReader = new Ext.data.JsonReader(
		{
			root: 'playlist.entry',
			idProperty: 'id',
			fields: Ext.ux.mattgoldspink.subsonic.Fields
		}
	);
	this.folderJsonReader = new Ext.data.JsonReader(
		{
			root: 'directory.child',
			idProperty: 'id',
			fields: Ext.ux.mattgoldspink.subsonic.Fields
		}
	);
	this.searchJsonReader = new Ext.data.JsonReader(
		{
			root: 'searchResult2.match',
			idProperty: 'id',
			searchType: 'all',
			fields: Ext.ux.mattgoldspink.subsonic.Fields
		}
	);
	this.searchJsonReader.preProcessResponse = function(o) {
		var searchResult2 = o.searchResult2, results = [], searchType = this.searchType;
		if (searchType === 'all' || searchType === 'artist' && searchResult2.artist) {
			var artists = searchResult2.artist;
			results = results.concat(Ext.isArray(artists) ? artists : [artists]);
		}
		if (searchType === 'all' || searchType === 'album' && searchResult2.album) {
			var albums = searchResult2.album;
			results = results.concat(Ext.isArray(albums) ? albums : [albums]);
		}
		if (searchType === 'all' || searchType === 'song' && searchResult2.song) {
			var songs = searchResult2.song;
			results = results.concat(Ext.isArray(songs) ? songs : [songs]);
		}
		o.searchResult2.match = results;
	};
};

Ext.apply(Ext.ux.mattgoldspink.subsonic.controllers.Store.prototype, {
    getWebWorkerStore: function() {
        return Ext.ux.mattgoldspink.subsonic.FolderTracksStore;
    },
    getFolderViewStore: function() {
        return Ext.ux.mattgoldspink.subsonic.FolderViewStore;
    },
	handleSearch: function(topic, data) {
		this.getWebWorkerStore().search(data.text, data.searchType);
		this.getFolderViewStore().on('load', function(store, recs, options) {
			this.getFolderViewStore().reader = options.previousReader;
			this.getFolderViewStore().proxy.setUrl(options.previousUrl);
		}, this, {
			single: true,
			previousReader: this.getFolderViewStore().reader,
			previousUrl: this.getFolderViewStore().proxy.url
		});
		this.searchJsonReader.searchType =  data.searchType;
		this.getFolderViewStore().reader = this.searchJsonReader;
		this.getFolderViewStore().proxy.setUrl(Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/search2.view');
		this.getFolderViewStore().load({
			params: {
				query: data.text + '*',
				artistCount : (data.searchType === 'all' || data.searchType === 'artist' ? 200 : 0),
				albumCount :  (data.searchType === 'all' || data.searchType === 'album' ? 200 : 0),
				songCount : (data.searchType === 'all' || data.searchType === 'song' ? 200 : 0)
			}
		});
	},
    handleFolderClick: function(topic, data) {
        if (data.playlist) {
			this.getWebWorkerStore().setPlaylistMode();
			this.getFolderViewStore().proxy.setUrl(Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getPlaylist.view');
			this.getFolderViewStore().reader = this.playlistJsonReader;
        } else {
			this.getFolderViewStore().proxy.setUrl(Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getMusicDirectory.view');
			this.getFolderViewStore().reader = this.folderJsonReader;
			this.getWebWorkerStore().setFolderMode();
        }
        window.Ext.ux.Bus.fireEvent('message', 'subsonic.load.store', data);
    },
    handleLoadStore: function(topic, data){
		this.getFolderViewStore().load({
			params: {
				id: data.id
			}
		});
        this.getWebWorkerStore().load({
            params: {
                id: data.id
            }
        });
    }
});

Ext.ux.mattgoldspink.subsonic.controllers.Store.singleton = new Ext.ux.mattgoldspink.subsonic.controllers.Store();
